package com.simtop.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * @Author: liuzhaobo
 * @Description: 返回值封装对象
 * @DateTime: 2022/8/31 0031 17:01
 * @Params:
 * @Return
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServerResponses<T> {
    private int status;
    private String msg;
    private T url;

    private ServerResponses(int status) {
        this.status = status;
    }

    private ServerResponses(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private ServerResponses(int status, T data) {
        this.status = status;
        this.url = data;
    }

    private ServerResponses(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.url = data;
    }

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return url;
    }

    //是否是一个正确的响应
    @JsonIgnore
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    //返回成功的响应对象
    public static <T> ServerResponses<T> createBySuccess() {
        return new ServerResponses<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponses<T> createBySuccessMsg(String msg) {
        return new ServerResponses<T>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> ServerResponses<T> createBySuccess(T data) {
        return new ServerResponses<T>(ResponseCode.SUCCESS.getCode(), data);
    }


    public static <T> ServerResponses<T> createBySuccess(String msg, T data) {
        return new ServerResponses<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }


    //响应失败时候调用的方法
    public static <T> ServerResponses<T> createByError() {
        return new ServerResponses<T>(ResponseCode.ERROR.getCode());
    }

    public static <T> ServerResponses<T> createByError(String msg, T data) {
        return new ServerResponses<T>(ResponseCode.ERROR.getCode(), msg, data);
    }

    public static <T> ServerResponses<T> createByErrorMsg(String msg) {
        return new ServerResponses<T>(ResponseCode.ERROR.getCode(), msg);
    }


    public static <T> ServerResponses<T> createByErrorCodeMsg(int errorCode, String errorMsg) {
        return new ServerResponses<T>(errorCode, errorMsg);
    }

    @Override
    public String toString() {
        return "ServerResponse{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + url +
                '}';
    }
}
